"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 硬件故障类--磁盘
Case Name   : 使用gs_dump工具导出过程中，磁盘空间不足
Create At   : 2020-10-20
Owner       : n@ningyali
Description :
    1、/home路径插入数据，使得空间剩余400M大小
    2、使用gs_dump进行导出tpcc数据库
    gs_dump -W ****** -f /home/zhangxb/backup/backup.sql -p 2900 tpcc -Z 8 -F p
    3、清理磁盘留出足够的空间，再进行导出
Expect      :
    1、故障注入成功
    2、导出失败，给出磁盘空间不足提示
    3、磁盘空间清理成功
    4、导出成功吧
History     :
"""
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import *
from testcase.utils.CommonSH import *


class BigArchFileAndDiskFull(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('---------------Opengauss_Reliability_Hardware_Case014:开始执行---------------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.primary_user_node = Node(node='PrimaryDbUser')
        self.standby1_root_node = Node(node='Standby1Root')
        self.standby1_user_node = Node(node='Standby1DbUser')
        self.standby2_root_node = Node(node='Standby2Root')
        self.standby2_user_node = Node(node='Standby2DbUser')
        self.com = Common()
        self.Constant = Constant()
        dump_file_name = 'backup.sql'
        self.back_up_path = os.path.join(macro.DB_BACKUP_PATH, dump_file_name)

        self.log.info("-----------获取主节点数据目录所在磁盘 -----------")
        cmd = f"df -h {os.path.dirname(self.back_up_path)}"
        self.log.info(cmd)
        msg = self.primary_root_node.sh(cmd).result()
        self.log.info(msg)
        self.db_disk_name = msg.splitlines()[-1].split()[0].strip()

    def test_main(self):
        self.log.info("-------------------注入磁盘满故障--------------------")
        cfe_command = f'rfile_full (diskname) values ({self.db_disk_name})'
        cfe_msg = self.com.cfe_inject(self.primary_root_node, cfe_command)
        self.log.info(cfe_msg)
        self.assertIn(self.Constant.CFE_DISK_FULL_SUCCESS_MSG, cfe_msg)

        self.log.info("-------------使用gs_dump进行导出tpcc数据库---------------")
        dump_result = self.primary_sh.dumpFile(self.back_up_path, get_detail=True)
        self.log.info(dump_result)
        self.assertIn(self.Constant.DISK_FULL_MSG, dump_result)

        self.log.info("---------------清理磁盘留出足够的空间----------------")
        cfe_command = f"rfile_full where (diskname={self.db_disk_name})"
        cfe_msg = self.com.cfe_clean(self.primary_root_node, cfe_command)
        self.log.info(cfe_msg)
        self.assertIn(self.Constant.CFE_DISK_CLEAN_SUCCESS_MSG, cfe_msg)

        self.log.info("-------------使用gs_dump进行导出tpcc数据库---------------")
        dump_result = self.primary_sh.dumpFile(self.back_up_path, get_detail=False)
        self.log.info(dump_result)
        self.assertTrue(dump_result)

    def tearDown(self):
        cfe_command = f"rfile_full where (diskname={self.db_disk_name})"
        cfe_msg = self.com.cfe_clean(self.primary_root_node, cfe_command)
        self.log.info(cfe_msg)
        self.assertIn(self.Constant.CFE_DISK_CLEAN_SUCCESS_MSG, cfe_msg)

        self.log.info("-----------删除备份文件-----------")
        shell_cmd = f"rm -rf {self.back_up_path}; ls -l {self.back_up_path}"
        self.log.info(shell_cmd)
        msg = self.primary_root_node.sh(shell_cmd).result()
        self.log.info(msg)

        self.log.info('---------------Opengauss_Reliability_Hardware_Case014:执行完成---------------')
